package com.insurance.modules.partner.controller;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.insurance.common.enums.StateEnum;
import com.insurance.entity.PartnerInfoRoleEntity;
import com.insurance.entity.PartnerRoleEntity;
import com.insurance.service.PartnerInfoRoleService;
import com.insurance.service.PartnerRoleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.insurance.common.utils.PageUtils;
import com.insurance.common.utils.R;



/**
 * 合作方角色关系表
 *
 * @date 2021-02-27 22:53:04
 */
@Slf4j
@RestController
@RequestMapping("partner/partnerinforole")
public class PartnerInfoRoleController {

    @Autowired
    private PartnerRoleService partnerRoleService;

    @Autowired
    private PartnerInfoRoleService partnerInfoRoleService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("product:partnerinforole:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = partnerInfoRoleService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{irId}")
    @RequiresPermissions("product:partnerinforole:info")
    public R info(@PathVariable("irId") String irId){
		PartnerInfoRoleEntity partnerInfoRole = partnerInfoRoleService.getById(irId);
        return R.ok().put("partnerInfoRole", partnerInfoRole);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("product:partnerinforole:save")
    public R save(@RequestBody PartnerInfoRoleEntity partnerInfoRole){
        partnerInfoRole.setIrIdText(UUID.randomUUID().toString().replaceAll("-", ""));
		partnerInfoRoleService.save(partnerInfoRole);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("product:partnerinforole:update")
    public R update(@RequestBody PartnerInfoRoleEntity partnerInfoRole){
		partnerInfoRoleService.updateById(partnerInfoRole);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("product:partnerinforole:delete")
    public R delete(@RequestBody Long[] irIds){
		partnerInfoRoleService.removeByIds(Arrays.asList(irIds));

        return R.ok();
    }

    /**
     * 获取所有的角色
     *
     * @param partnerId
     * @return
     */
    @RequestMapping(value = "/getRoleListByPartnerId/{partnerId}")
    public R getRoleListByPartnerId(@PathVariable String partnerId){
        List<String> roleIdList = partnerInfoRoleService.getRoleStrListByPartnerId(partnerId);
        QueryWrapper<PartnerRoleEntity> queryWrapper = partnerRoleService.getQueryWrapper(new PartnerRoleEntity());
        queryWrapper.eq("state", StateEnum.UP.getCode());
        List<PartnerRoleEntity> roleList = partnerRoleService.queryList(queryWrapper);
        if(CollectionUtils.isNotEmpty(roleList)){
            for(PartnerRoleEntity partnerRoleEntity : roleList){
                if(roleIdList.contains(partnerRoleEntity.getRoleIdText())){
                    partnerRoleEntity.setRoleChecked(true);
                }
            }
        }
        return R.ok().put("roleList", roleList);
    }

    @RequestMapping("/configRole")
    public R configRole(String partnerId, String roleIdList){
        try {
            if(StringUtils.isBlank(partnerId)){
                return R.error();
            }
            List<PartnerInfoRoleEntity> partnerInfoRoleEntityList = new ArrayList<PartnerInfoRoleEntity>();
            if(!StringUtils.isBlank(roleIdList)){
                String[] roleIds = roleIdList.split(",");
                if(roleIds != null && roleIds.length > 0){
                    for (int i = 0; i < roleIds.length; i++) {
                        partnerInfoRoleEntityList.add(new PartnerInfoRoleEntity(partnerId, roleIds[i]));
                    }
                }
            }
            boolean flag = partnerInfoRoleService.saveAndDelete(partnerId, partnerInfoRoleEntityList);
            return flag ? R.ok() : R.error();
        } catch (Exception e) {
            log.error("【配置角色失败, 系统异常】", e);
            return R.error();
        }
    }

}
